package com.ruoyi.job.task;

import breeze.framework.string.Str;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.job.domain.BzDataEntity;
import com.ruoyi.job.domain.BzFlowDay;
import com.ruoyi.job.domain.BzIpEntity;
import com.ruoyi.job.domain.ParameterEntity;
import com.ruoyi.job.mapper.IBzFlowDayMapper;
import com.ruoyi.job.service.IBzDataService;
import com.ruoyi.job.service.IBzFlowDayService;
import com.ruoyi.job.service.IBzIpService;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.utils.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    @Resource
    private IBzIpService bzIpService;
    @Resource
    private IBzDataService bzDataService;
    @Resource
    private IBzFlowDayService bzFlowDayService;

    public void calBzTotalFlow(){
        LocalDate now = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime yesterdayStart = LocalDateTime.of(now.minusDays(1), LocalTime.MIN);
        LocalDateTime yesterdayEnd = LocalDateTime.of(now, LocalTime.MAX);
        List<BzIpEntity> bzIpEntities = bzIpService.list();
        bzIpEntities.forEach(bzIpEntity -> {
            String ip = bzIpEntity.getIp();
            BzDataEntity bzDataStart = bzDataService.getFirst(yesterdayStart, yesterdayEnd, ip);
            if(bzDataStart == null){
                return;
            }
            List<ParameterEntity> startList = bzDataStart.getJsonData().toList(ParameterEntity.class);
            ParameterEntity startEntity = startList.stream().filter(parameterEntity -> parameterEntity.getParameterKey().equals("ZLJ")).findFirst().orElse(null);
            if(startEntity == null){
                return;
            }
            BzDataEntity bzDataEnd = bzDataService.getEnd(yesterdayStart, yesterdayEnd, ip);
            if(bzDataEnd == null){
                return;
            }
            List<ParameterEntity> endList = bzDataEnd.getJsonData().toList(ParameterEntity.class);
            ParameterEntity endEntity = endList.stream().filter(parameterEntity -> parameterEntity.getParameterKey().equals("ZLJ")).findFirst().orElse(null);
            if(endEntity == null){
                return;
            }
            BigDecimal totalFlow = endEntity.getParameterValue().subtract(startEntity.getParameterValue());
            BzFlowDay bzFlowDay = BzFlowDay.builder()
                    .dayFinalFlow(endEntity.getParameterValue())
                    .totalFlow(totalFlow)
                    .dayStr(df.format(yesterdayStart))
                    .createTime(LocalDateTime.now())
                    .ip(bzIpEntity.getIp())
                    .ipName(bzIpEntity.getName())
                    .build();
            long count = bzFlowDayService.countByIpAndDayStr(ip,df.format(yesterdayStart));
            if(count == 0){
                bzFlowDayService.save(bzFlowDay);
            }else{
                bzFlowDayService.updateByIpAndDayStr(bzFlowDay, ip, df.format(yesterdayStart));
            }
        });
    }
}
